Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_PERFORMANCE_DATA = &H80000004
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_DYN_DATA = &H80000006
Const REG_SZ = 1 ' Unicode nul terminated string
Const REG_BINARY = 3 ' Free form binary
Const REG_DWORD = 4 ' 32-bit number
Const ERROR_SUCCESS = 0&
Enum RegHKey
Classes_Root = 0
Current_User = 1
Local_Machine = 2
Users = 3
Current_Config = 4
Dyn_Data = 5
End Enum
Public regKey As RegHKey
Private mKey As Long
Private Function GetKey(hKey As RegHKey)
If hKey = Classes_Root Then
GetKey = HKEY_CLASSES_ROOT
ElseIf hKey = Current_Config Then
GetKey = HKEY_CURRENT_CONFIG
ElseIf hKey = Current_User Then
GetKey = HKEY_CURRENT_USER
ElseIf hKey = Dyn_Data Then
GetKey = HKEY_DYN_DATA
ElseIf hKey = Local_Machine Then
GetKey = HKEY_LOCAL_MACHINE
ElseIf hKey = Users Then
GetKey = HKEY_USERS
End If
End Function
Public Sub CreateKey(hKey As RegHKey, strPath As String)
Dim hCurKey As Long
Dim lRegResult As Long
hKey = GetKey(hKey)
lRegResult = RegCreateKey(hKey, strPath, hCurKey)
If lRegResult <> ERROR_SUCCESS Then
' there is a problem
End If
lRegResult = RegCloseKey(hCurKey)
End Sub
Public Sub DeleteKey(ByVal hKey As RegHKey, ByVal strPath As String)
Dim lRegResult As Long
hKey = GetKey(hKey)
lRegResult = RegDeleteKey(hKey, strPath)
End Sub
Public Sub DeleteValue(ByVal hKey As RegHKey, ByVal strPath As String, ByVal strValue As String)
Dim hCurKey As Long
Dim lRegResult As Long
hKey = GetKey(hKey)
lRegResult = RegOpenKey(hKey, strPath, hCurKey)
lRegResult = RegDeleteValue(hCurKey, strValue)
lRegResult = RegCloseKey(hCurKey)
End Sub
Public Function GetSettingString(hKey As RegHKey, strPath As String, strValue As String, Optional Default As String) As String